/*
var albumWindow = {
  photosHtml:null,
  uploadPhotoHtml:"<div class='userPhotos' style='background-image:url(/pie/image/uploadPhoto.png)'><form id='uploadPhotoForm'  style='height:100%;'>"
    +"<input type='file' name='photo' onchange='albumWindow.uploadPhoto(this.files)' style='opacity:0;width:100%;height:100%;'/>"
    +"<input type='text' id='tcpId' name='id' style='display:none'/><input type='text'"
      +"id='userAccount' style='display:none' name='account'/></form></div>",
  init:function(){
    jQuery('#personName').html(user.account);
    if(user.headIndex >= 0){
      jQuery('#personHead').css('background-image','url('+user.photos[user.headIndex]+')');
    }
    else{
      jQuery('#personHead').css('background-image','url(/pie/image/headDefault.png)');
    }
    albumWindow.photosHtml = "";
    for(var i = 0; i < user.photos.length; i++){
      albumWindow.photosHtml += "<div class='userPhotos' id='userPhoto"+i+"' style='background-image:url("+user.photos[i]
      +")' onclick='albumWindow.select("+i+")'/></div>";
    }
    jQuery('#photosNav').html(albumWindow.photosHtml+albumWindow.uploadPhotoHtml);
    jQuery('#tcpId').val(tcp.id);
    jQuery('#userAccount').val(user.account);
  },
  resize:function(){
    var mainSize = myBody.width*(1/3);
    jQuery('#currentPhoto').css('width',mainSize+'px');
    jQuery('#currentPhoto').css('height',mainSize+'px');
    jQuery('#photosNav').css('height',mainSize*(4/5)+'px');
    jQuery('#photosNav').css('margin-top',mainSize*(1/10)+'px');
  },
  show:function(){
    if(albumWindow.display){return;}
    setTimeout(function(){
      albumWindow.display = true;
      jQuery('#albumWindow').css('display','block');
      var mLeft = parseInt(jQuery('#albumWindow').css('margin-left'));
      var goal = (myBody.width - parseInt(jQuery('#albumWindow').css('width')))/2;
      albumWindow.moveRight = setTimeout(animate,25);
      function animate(){
        if(mLeft <= goal-100){pielogo.moveRight();}
        if(goal-mLeft<=30){
          jQuery('#albumWindow').css('margin-left',goal+'px');
          return;
        }
        mLeft += 30;
        jQuery('#albumWindow').css('margin-left',mLeft+'px');
        albumWindow.moveRight = setTimeout(animate,25);
      }
    },25);
  },
  hide:function(){

  },
  select:function(n){

  },
  uploadPhoto:function(files){
    var imageMode = /^[\s\S]+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$/;
    if(!imageMode.test(files[0].name)){
      alert('请上传png,jpg,peg,gif,bmp格式的图像')
      return;
    }
    var xhr = new XMLHttpRequest();
    xhr.open('POST','/uploadPhoto');
    var form = document.getElementById('uploadPhotoForm');
    xhr.send(new FormData(form));
  },
}
<div id='albumWindow' class='hiddenWindow'>
      <div id='personInfo'>
        <div id='personHead'></div>
        <div id='personName'></div>
      </div>
      <div id='personInfoNav'>
      </div>
      <div id='currentPhoto'>
      </div>
</div>
*/
jQuery('body').ready(init);
function init(){
	body.resize();
	homePage.show();
	if(localStorage.dcjiaAccount){
		jQuery('#loginAccountInput').val(localStorage.dcjiaAccount);
		jQuery('#loginPasswordInput').val(localStorage.dcjiaPassword);
		loginPage.submit();
	}
	jQuery('#confirmBoxCancelButton').bind('click',function(){
		jQuery('#confirmBox').css('display','none');
	})
}
var tcp = {
	handle:null,
	id:null
}
var myPage = {
	width:0,
	height:0
}
var user = {
	account:null,
	nickname:null,
	sign:null,
	photos:null,
	headIndex:-1,
	friendsAccount:null,
	friends:[],
	msgRecords:[],
	init:function(){
		for(var i = 0; i < user.msgRecords.length; i++){
			var data = user.msgRecords[i];
			switch(data.name){
				case 'friendsRequest':
					if(data.flag != 1){return;}
					body.showConfirm(data.content+'('+data.from+')请求添加你为好友，是否同意?',function(){
						tcp.handle.emit('friendAgree',{'target':data.from});
						var len = user.friendsAccount.length;
						user.friendsAccount.push(data.from);
						user.friends.push(friendModel.createNew(data.from,'','',null,-1,false));
						jQuery('#friendsPage').append("<div class='friends'><a style='position:absolute;opacity:0' name='friends"+len+"'></a>"
										+"<div class='friendsHead' id='friends"+len+"Head'></div>"
			              				+"<div class='friendsNickname' id='friends"+len+"Nickname'></div>"
			              				+"<div class='friendsAccount' id='friends"+len+"Account'></div>"
			              				+"<div class='friendsSign' id='friends"+len+"Sign'></div></div>");
						tcp.handle.emit('askFriendsInfo',{'fAccount':data.from
												 ,'fIndex':len});
					})
					break;
				case 'newFriends':
					body.showMessage(data.content+'('+data.from+')同意了你的好友请求',1);
					var len = user.friendsAccount.length;
					user.friendsAccount.push(data.from);
					user.friends.push(friendModel.createNew(data.from,'','',null,-1,false));
					jQuery('#friendsPage').append("<div class='friends'><a style='position:absolute;opacity:0' name='friends"+id+"'></a>"
										+"<div class='friendsHead' id='friends"+len+"Head'></div>"
			              				+"<div class='friendsNickname' id='friends"+len+"Nickname'></div>"
			              				+"<div class='friendsAccount' id='friends"+len+"Account'></div>"
			              				+"<div class='friendsSign' id='friends"+len+"Sign'></div></div>");
					tcp.handle.emit('askFriendsInfo',{'fAccount':data.from
												 ,'fIndex':len});
					break;
				case 'newMsg':
					var msg = {
						'from':data.from,
						'content':data.content[0],
						'time':data.content[1]
					}
					conversationPage.receiveMsg(msg)
				    break;
				default:break;
			}
		}
	}
}
var friendModel = {
	createNew:function(account,nickname,sign,photos,headIndex,online){
		var friend = {};
		friend.account = account;
		friend.nickname = nickname;
		friend.sign = sign;
		friend.photos = photos;
		friend.headIndex = headIndex;
		friend.online = online;
		return friend;
	}
}
var body = {
	currentPage:'homePage',
	needCheckLeave:false,
	resize:function(){
		myPage.height = window.innerHeight;
		myPage.width = window.innerWidth;
		jQuery('#all').css('width',myPage.width);
		jQuery('#fixedTop').css('height',fixedTop.height+'px');
		jQuery('#fixedBottom').css('height',fixedBottom.height+'px');
		jQuery('#middle').css('height',myPage.height-fixedTop.height-fixedBottom.height+'px');
		jQuery('.page').css('height',myPage.height-fixedTop.height-fixedBottom.height+'px');
		jQuery('.innerShadow').css('width',(myPage.width*0.8)+'px');
		jQuery('.innerShadow').css('margin-left',(myPage.width*0.1)+'px');
		jQuery('.floatBox').css('margin-left',(myPage.width-200)/2+'px');
		fixedBottom.resize();   //上下两句顺序不能调换，原因是navButton的长度问题
		fixedTop.resize();
		homePage.resize();
		loginPage.resize();
		registerPage.resize();
		conversationPage.resize();
		friendsPage.resize();
		albumPage.resize();
		gamePage.resize();
		settingsPage.resize();
	},
	rotate:function(){
		//body.showMessage(window.orientation,0);
	},
	checkLeave:function(){
		if(!body.needCheckLeave){return;}
		clearTimeout(tcp.timingReload);
		tcp.timingReload = setTimeout(function(){
			body.needCheckLeave = false;
			window.location.reload();
		},75000);
		tcp.handle.emit('timingReply');
		return '真的要走么？';
	},
	showMessage:function(msg,status){
	   //因为alert会阻塞执行，不能采用,所以自己实现一个提示消息的浮动框,要能够自动消失
	   switch(status){
	   	case 0:    //坏消息
	   		jQuery('#readonlyMessage').css('color','rgb(240,86,84)');
	   		break;
	   	case 1:   //好消息
	   		jQuery('#readonlyMessage').css('color','rgb(0,229,0)');
	   		break;
	   	default:
	   		break;
	   }
	   jQuery('#readonlyMessage').html(msg);
	   jQuery('#messageBox').fadeIn(500);
	   setTimeout(function(){
	   	jQuery('#messageBox').fadeOut(2000);
	   },2000);
	},
	showConfirm:function(msg,callback){
	   //因为confirm也会阻塞执行，不能采用,所以自己实现一个验证消息的浮动框,但不能够自动消失
	   jQuery('#confirmMessage').html(msg);
	   jQuery('#confirmBox').css('display','block');
	   jQuery('#confirmBoxConfirmButton').unbind();
	   jQuery('#confirmBoxConfirmButton').bind('click',function(){
	   	jQuery('#confirmBox').css('display','none');
	   	callback();
	   });
	},
	showPrompt:function(msg,defaultValue,callback){
		jQuery('#promptMessage').html(msg);
		jQuery('#promptInput').val(defaultValue);
	   	jQuery('#promptBox').css('display','block');
	   	jQuery('#promptInput').focus();
	   	jQuery('#promptBoxConfirmButton').bind('click',function(){
	   		jQuery('#promptBox').css('display','none');
	   		callback(jQuery('#promptInput').val())
		})
	   	jQuery('#promptBoxCancelButton').bind('click',function(){
	   		jQuery('#promptBox').css('display','none');
	   		callback(null);
	   	})
	}
}
var fixedTop = {
	height:60,
	clicked:null,
	resize:function(){
		jQuery('#topLeft').css('width',myPage.width*(1/4)+'px');
		jQuery('#topCenter').css('width',myPage.width*(1/2)+'px');
		jQuery('#topRight').css('width',myPage.width*(1/4)+'px');
	},
	hidden:function(){
		if(jQuery('#fixedTop').css('display') == 'none'){return;}
		jQuery('#fixedTop').css('display','none');
		var middleHeight = parseInt(jQuery('#middle').css('height'));
		middleHeight +=  fixedTop.height;
		jQuery('#middle').css('height',middleHeight+'px');
		jQuery('.page').css('height',middleHeight+'px');
		jQuery('.innerPage').css('height',middleHeight+'px');
	},
	show:function(){
		if(jQuery('#fixedTop').css('display') == 'block'){return;}
		jQuery('#fixedTop').css('display','block');
		var middleHeight = parseInt(jQuery('#middle').css('height'));
		middleHeight -= fixedTop.height;
		jQuery('#middle').css('height',middleHeight+'px');
		jQuery('.page').css('height',middleHeight+'px');
		jQuery('.innerPage').css('height',middleHeight+'px');
	},
	set:function(left,center,right){
		jQuery('#topLeftNav').html(left);
		jQuery('#topCenterNav').html(center);
		jQuery('#topRightNav').html(right);
		if(left == ''){
			jQuery('#topLeftNav').css('background-color','transparent');
		}
		else{
			jQuery('#topLeftNav').css('background-color','rgb(19,120,151)');
		}
		if(right == ''){
			jQuery('#topRightNav').css('background-color','transparent');
		}
		else{
			jQuery('#topRightNav').css('background-color','rgb(19,120,151)');
		}
	}
}
var fixedBottom = {
	height:60,
	choosed:null,
	resize:function(){
		jQuery('.navButtons').css('width',myPage.width/4+'px');
		jQuery('#settingsNav').css('width',myPage.width/4-4+'px');
	},
	hidden:function(){
		if(jQuery('#fixedBottom').css('display') == 'none'){return;}
		jQuery('#fixedBottom').css('display','none');
		var middleHeight = parseInt(jQuery('#middle').css('height'));
		middleHeight += fixedBottom.height;
		jQuery('#middle').css('height',middleHeight+'px');
		jQuery('.page').css('height',middleHeight+'px');
		jQuery('.innerPage').css('height',middleHeight+'px');
	},
	show:function(){
		if(jQuery('#fixedBottom').css('display') == 'block'){return;}
		jQuery('#fixedBottom').css('display','block');
		var middleHeight = parseInt(jQuery('#middle').css('height'));
		middleHeight -= fixedBottom.height;
		jQuery('#middle').css('height',middleHeight+'px');
		jQuery('.page').css('height',middleHeight+'px');
		jQuery('.innerPage').css('height',middleHeight+'px');
	},
}
var homePage = {
	resize:function(){
		jQuery('#homePage').css('height',myPage.height+'px');
		jQuery('#homeButtons').css('margin-top',(myPage.height-50)+'px');
	},
	show:function(){
		fixedTop.hidden();
		fixedBottom.hidden();
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'homePage';
		jQuery('#'+body.currentPage).css('display','block');
	}
}
var loginPage = {
	//logoInit:false,
	rotating:null,
	//logoSize:150,
	show:function(){
		fixedTop.set('首页','登 录','注册');
		fixedTop.show();
		fixedBottom.hidden();
		jQuery('#topLeftNav').unbind();
		jQuery('#topLeftNav').bind('click',function(){
			homePage.show();
		});
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			registerPage.show();
		});
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'loginPage';
		jQuery('#'+body.currentPage).css('display','block');
		jQuery('#loginAccountInput').focus();
		/*if(!loginPage.logoInit){
			loginPage.rotating = setTimeout(loginPage.drawLogo,25);   //以后改成用图片实现动八卦
			loginPage.logoInit = true;
		}*/
	},
	resize:function(){
		jQuery('#loginPage').css('height',(myPage.height-fixedTop.height)+'px');
	},
	drawLogo:function(){  //画八卦的部分
		var canvas = document.getElementById('logoCanvas');
		canvas.height = loginPage.logoSize;
		canvas.width = loginPage.logoSize;
		var ctx = canvas.getContext('2d');
		var R = 70;
		ctx.beginPath();
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize/2,R,0,2*Math.PI,true);
		ctx.lineWidth = 2;
		ctx.stroke();
		ctx.fillStyle='white';
		ctx.fill();
		ctx.lineWidth = 1;
		ctx.beginPath();
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize*(1/4)+3,R/2,(1/2)*Math.PI,(3/2)*Math.PI,true);
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize*(3/4)-2,R/2,(1/2)*Math.PI,(3/2)*Math.PI,false);
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize/2,R,(3/2)*Math.PI,(1/2)*Math.PI,false);
		ctx.fillStyle='black';
		ctx.fill();
		ctx.beginPath();
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize*(1/4)+3,10,0,2*Math.PI);
		ctx.fill();
		ctx.beginPath();
		ctx.arc(loginPage.logoSize/2,loginPage.logoSize*(3/4)-2,10,0,2*Math.PI);
		ctx.fillStyle='white';
		ctx.fill();
		//fixedBottom.rotating = setTimeout(loginPage.drawLogo,25);  //以后改成用图片实现动八卦
	},
	doLogin:function(){
		body.needCheckLeave = true;
		tcp.handle = io.connect('http://localhost');
		tcp.handle.on('connected',function(data){
			tcp.timingReload = setTimeout(function(){
				body.needCheckLeave = false;
				window.location.reload();
			},75000);
			tcp.id = data.id;
			tcp.handle.emit('connectReply',{account:user.account});
			tcp.handle.on('userInfo',function(data){
				user.headIndex = data.headIndex;
				user.nickname = data.nickname;
				user.sign = data.sign;
				user.friendsAccount = data.friendsAccount;
				user.photos = data.photos;
				user.msgRecords = data.msgRecords;
				if(user.headIndex == -1){
					jQuery('#personHead').css('background-image','url(/images/headDefault.png)');
				}
				else{
					jQuery('#personHead').css('background-image','url('+user.photos[user.headIndex]+')');
				}
				if(user.nickname){
					jQuery('#settingsNicknameInput').val(user.nickname);
				}
				if(user.sign){
					jQuery('#settingsSignatureInput').val(user.sign);
				}
				friendsPage.init();
				albumPage.init();
				user.init();
			});
			tcp.handle.on('newPhoto',function(data){
				user.photos.push(data.photoUrl);
				albumPage.init();
			})
			tcp.handle.on('updateInformationSuccess',function(){
				body.showMessage('提交成功',1);
			})
			tcp.handle.on('friendsRequest',function(data){
				var newFriendNickname = data.nickname?data.nickname:'未命名';
				body.showConfirm(newFriendNickname+'('+data.from+')请求添加你为好友，是否同意?',function(){
					tcp.handle.emit('friendAgree',{'target':data.from});
					var len = user.friendsAccount.length;
					user.friendsAccount.push(data.from);
					user.friends.push(friendModel.createNew(data.from,'','',null,-1,false));
					jQuery('#friendsPage').append("<div class='friends' onclick='conversationPage.talkWith("+len
										+",1)'><a style='position:absolute;opacity:0' name='friends"+len+"'></a>"
										+"<div class='friendsHead' id='friends"+len+"Head'></div>"
			              				+"<div class='friendsNickname' id='friends"+len+"Nickname'></div>"
			              				+"<div class='friendsAccount' id='friends"+len+"Account'></div>"
			              				+"<div class='friendsSign' id='friends"+len+"Sign'></div></div>");
					tcp.handle.emit('askFriendsInfo',{'fAccount':data.from
												 ,'fIndex':len});
				})
			})
			tcp.handle.on('newFriend',function(data){
				var newFriendNickname = data.nickname?data.nickname:'未命名';
				body.showMessage(newFriendNickname+'('+data.account+')同意了你的好友请求',1);
				var len = user.friendsAccount.length;
				user.friendsAccount.push(data.account);
				user.friends.push(friendModel.createNew(data.account,'','',null,-1,false));
				jQuery('#friendsPage').append("<div class='friends' onclick='conversationPage.talkWith("+len
										+",1)'><a style='position:absolute;opacity:0' name='friends"+len+"'></a>"
										+"<div class='friendsHead' id='friends"+len+"Head'></div>"
			              				+"<div class='friendsNickname' id='friends"+len+"Nickname'></div>"
			              				+"<div class='friendsAccount' id='friends"+len+"Account'></div>"
			              				+"<div class='friendsSign' id='friends"+len+"Sign'></div></div>");
				tcp.handle.emit('askFriendsInfo',{'fAccount':data.account
												 ,'fIndex':len});
			})
			tcp.handle.on('friendInfo',function(data){
				user.friends[data.fIndex].nickname = data.nickname?data.nickname:'未命名';
				user.friends[data.fIndex].sign = data.sign?data.sign:'这个家伙什么都没留下';
				user.friends[data.fIndex].photos = data.photos;
				user.friends[data.fIndex].headIndex = data.headIndex;
				user.friends[data.fIndex].online = data.online;
				var friendHeadUrl = '/images/headDefault.png';
				if(data.headIndex >= 0){
					friendHeadUrl = data.photos[data.headIndex];
				}
				jQuery('#conversationHead'+data.fIndex).css('background-image','url('+friendHeadUrl+')');
				jQuery('#friends'+data.fIndex+'Head').css('background-image','url('+friendHeadUrl+')');
				jQuery('#friends'+data.fIndex+'Nickname').html(user.friends[data.fIndex].nickname);
				jQuery('#conversationName'+data.fIndex).html(user.friends[data.fIndex].nickname);
				jQuery('#friends'+data.fIndex+'Account').html(user.friendsAccount[data.fIndex]);
				jQuery('#friends'+data.fIndex+'Sign').html(user.friends[data.fIndex].sign);
				if(!data.online){   //好友不在线
					jQuery('#friends'+data.fIndex+'Nickname').css('color','black');
				}
			})
			tcp.handle.on('newMsg',function(data){
				conversationPage.receiveMsg(data);
			})
			tcp.handle.on('friendOnline',function(data){
				var fIndex = -1;
				for(var i = 0; i < user.friendsAccount.length; i++){
					if(user.friendsAccount[i] == data.account){
						fIndex = i;
						break;
					}
				}
				if(fIndex < 0){
					body.showMessage('一个未知好友发来他的上线消息,他是:'+data.account);
					return;
				}
				user.friends[fIndex].online = true;
				jQuery('#friends'+fIndex+'Nickname').css('color','rgb(255,215,0)');
				jQuery('#conversationName'+fIndex).css('color','rgb(255,215,0)');
			})
			tcp.handle.on('friendOffline',function(data){
				var fIndex = -1;
				for(var i = 0; i < user.friendsAccount.length; i++){
					if(user.friendsAccount[i] == data.account){
						fIndex = i;
						break;
					}
				}
				if(fIndex < 0){
					body.showMessage('一个未知好友发来他的下线消息,他是:'+data.account);
					return;
				}
				user.friends[fIndex].online = false;
				jQuery('#friends'+fIndex+'Nickname').css('color','black');
				jQuery('#conversationName'+fIndex).css('color','black');
			})
			tcp.handle.on('timingCheck',function(){
				clearTimeout(tcp.timingReload);
				tcp.timingReload = setTimeout(function(){
					body.needCheckLeave = false;
					window.location.reload();
				},75000);
				tcp.handle.emit('timingReply');
			})
			tcp.handle.on('error',function(data){
				body.showMessage('出错啦~~~'+data.detail+',请联系管理员',0);
			})
			tcp.handle.on('logout',function(data){
				body.showMessage(''+data.info+'马上为您尝试重新连接',0);
				clearTimeout(tcp.timingReload);
				tcp.timingReload = setTimeout(function(){
					body.needCheckLeave = false;
					window.location.reload();
				},3000);
			})
			tcp.handle.on('bumped',function(){
				body.needCheckLeave = false;
				window.location.reload();
			})
		});
	},
	submit:function(){
		var accountMode = /^[0-9]{11}$/;
		var account = jQuery('#loginAccountInput').val();
		if(!accountMode.test(account)){
			body.showMessage('账号不存在',0);
			return;
		}
		var passwordMode = /^[a-zA-Z0-9]{6,16}$/;
		var password = jQuery('#loginPasswordInput').val();
		if(!passwordMode.test(password)){
			body.showMessage('密码错误',0);
			return;
		}
		var loginData = {
			'account':account,
			'password':password
		}
		jQuery.ajax({
			url:'/login',
			type:'post',
			data:loginData,
			dataType:'json',
			success:function(reply){
				switch(reply.result){
					case -2:
						body.showMessage('账号不存在',0);
						break;
					case -1:
						body.showMessage('未知错误，请联系管理员',0);
						break;
					case 0:
						body.showMessage('账号或密码错误',0);
						break;
					case 1:
						localStorage.dcjiaAccount = account;
						localStorage.dcjiaPassword = password;
						user.account = account;
						loginPage.doLogin();
						friendsPage.show();
						break;
					case 2:
						body.showConfirm('账号已在别处登录,这也可能是因为你刚下线服务器还没反应过来造成的,要继续登录吗？',function(){
							localStorage.dcjiaAccount = account;
							localStorage.dcjiaPassword = password;
							user.account = account;
							loginPage.doLogin();
							friendsPage.show();
						})
						break;
					default:
						body.showMessage('未知错误，请联系管理员',0);
						break;
				}
			},
			error:function(err){
				body.showMessage('你的网络连接状况不太行吧?连不上啊',0);
			}
		})
	},
}
var registerPage = {
	resize:function(){
		jQuery('#registerPage').css('height',(myPage.height-fixedTop.height)+'px');
	},
	show:function(){
		fixedTop.set('首页','注  册','登录');
		fixedTop.show();
		fixedBottom.hidden();
		jQuery('#topLeftNav').unbind();
		jQuery('#topLeftNav').bind('click',function(){
			homePage.show();
		});
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			loginPage.show();
		});
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'registerPage';
		jQuery('#'+body.currentPage).css('display','block');
		jQuery('#registerAccountInput').focus();
	},
	submit:function(){
		var accountMode = /^[0-9]{11}$/;
		var account = jQuery('#registerAccountInput').val();
		if(!accountMode.test(account)){
			body.showMessage('请输入11位手机号作为账号',0);
			return;
		}
		var passwordMode = /^[a-zA-Z0-9]{6,16}$/;
		var password = jQuery('#registerPasswordInput').val();
		if(!passwordMode.test(password)){
			body.showMessage('请输入6~16位字符作为密码',0);
			return;
		}
		var passwordRepeat = jQuery('#registerPasswordRepeatInput').val();
		if(password != passwordRepeat){
			body.showMessage('请输入相同密码',0);
			return;
		}
		var regData = {
			'account':account,
			'password':password
		}
		jQuery.ajax({
			url:'/register',
			type:'post',
			data:regData,
			dataType:'json',
			success:function(reply){
				switch(reply.result){
					case -1:
						body.showMessage('未知错误，请联系管理员',0);
						break;
					case 0:
						body.showMessage('账号已被注册',0);
						break;
					case 1:
						localStorage.dcjiaAccount = account;
						localStorage.dcjiaPassword = password;
						user.account = account;
						settingsPage.show();
						loginPage.doLogin();
						break;
					default:
						body.showMessage('未知错误，请联系管理员',0);
						break;
				}
			},
			error:function(err){
				body.showMessage('未知错误，请联系管理员',0);
			}
		})
	},
}
var conversationPage = {
	currentConversations:[],
	resize:function(){
		var pageHeight = parseInt(jQuery('#conversationPage').css('height'));
		jQuery('.conversationRecordArea').css('height',pageHeight-80+'px');
	},
	show:function(){
		fixedTop.set('','会 话','');
		fixedTop.show();
		fixedBottom.show();
		if(fixedTop.clicked){
			jQuery('#'+fixedTop.clicked).css('color','black');
		}
		fixedTop.clicked = 'conversationNav';
		jQuery('#'+fixedTop.clicked).css('color','rgb(255,215,0)');
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'conversationPage';
		jQuery('#'+body.currentPage).css('display','block');
		jQuery('.innerPage').css('display','none');
		jQuery('#conversationList').css('display','block');
		var pageHeight = parseInt(jQuery('#conversationPage').css('height'));
		jQuery('.conversationRecordArea').css('height',pageHeight-80+'px');
	},
	receiveMsg:function(data){
		var n = -1;
		for(var i = 0; i < user.friendsAccount.length; i++){
			if(user.friendsAccount[i] == data.from){
				n = i;
				break;
			}
		}
		if(n < 0){
			body.showMessage('收到不明来历的消息'+data.content+'来自'+data.from+'~~~'+data.time);
			return;
		}
		jQuery('#conversationNav').css('color','red');
		conversationPage.talkWith(n,0);
		var friendHeadUrl = '/images/headDefault.png';
		if(user.friends[n].headIndex >= 0){
			friendHeadUrl = user.friends[n].photos[user.friends[n].headIndex]
		}
		var friendNickname = '无名氏';
		if(user.friends[n].nickname != ''){
			friendNickname = user.friends[n].nickname;
		}
		var newMsg = "<div class='conversationRecord'><div class='conversationMsgHead' style='background-image:url("
					 +friendHeadUrl+")'></div><div class='conversationMsgTime'>"+data.time
					 +"</div><div class='conversationMsgContent'>"+data.content+"</div></div>";
		jQuery('#conversationRecordArea'+n).append(newMsg);
		jQuery('#conversationDemo'+n).html(':'+data.content);
		if(!user.friends[n].online){
			jQuery('#conversationName'+n).css('color','black');
		}
	},
	speak:function(n){
		var str = jQuery('#conversationInput'+n).val();
		jQuery('#conversationInput'+n).val('')
		if(str == ''){return;}
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth()+1;
		if(month < 10){
			month = '0'+month;
		}
		var date = now.getDate();
		if(date < 10){
			date = '0'+date;
		}
		var hour = now.getHours();
		if(hour < 10){
			hour = '0'+hour;
		}
		var minute = now.getMinutes();
		if(minute < 10){
			minute = '0'+minute;
		}
		var second = now.getSeconds();
		if(second < 10){
			second = '0'+second;
		}
		var time = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
		//body.showMessage('speak to ' + user.friends[n].nickname+'('+user.friendsAccount[n]+'):'+str,'1');
		var userHeadUrl = '/images/headDefault.png';
		if(user.headIndex >= 0){
			userHeadUrl = user.photos[user.headIndex]
		}
		var userNickname = '无名氏';
		if(user.nickname != ''){
			userNickname = user.nickname;
		}
		var newMsg = "<div class='conversationRecord'><div class='conversationMsgHead' style='background-image:url("
					 +userHeadUrl+")'></div><div class='conversationMsgTime'>"+time
					 +"</div><div class='conversationMsgContent'>"+str+"</div></div>";
		jQuery('#conversationRecordArea'+n).append(newMsg);
		jQuery('#conversationDemo'+n).html('');
		tcp.handle.emit('sendMsg',{'target':user.friendsAccount[n],'time':time,'content':str});
	},
	talkWith:function(n,flag){  //flag表示这次聊天的是否有用户主动发起，还有可能是对方发起的,由对方发起的会话不主动显示会话窗口
		for(var i = 0; i < conversationPage.currentConversations.length; i++){
			if(conversationPage.currentConversations[i] == n){
				if(flag == 1){
					conversationPage.show();
					fixedTop.set('后退',user.friends[n].nickname,'');
					jQuery('#topLeftNav').unbind();
					jQuery('#topLeftNav').bind('click',function(){
						conversationPage.show();
					});
					jQuery('#conversationList').css('display','none');
					jQuery('#conversation'+n).css('display','block');
				}
				return;
			}
		}
		conversationPage.currentConversations.push(n);
		var friendHeadUrl = '/images/headDefault.png';
		if(user.friends[n].headIndex >= 0){
			friendHeadUrl = user.friends[n].photos[user.friends[n].headIndex]
		}
		var friendNickname = '无名氏';
		if(user.friends[n].nickname != ''){
			friendNickname = user.friends[n].nickname;
		}
		var newConversationItem = "<div id='conversationItem"+n+"' class='conversationItem' onclick='conversationPage.talkWith("+n+",1)'>"
								 +"<div class='conversationHead' id='conversationHead"+n+"' style='background-image:url("+friendHeadUrl
								 +")'></div><div class='conversationName' id='conversationName"+n+"'>"+friendNickname
								 +"</div><div id='conversationDemo"+n+"' class='conversationDemo'></div></div>";
		jQuery('#conversationList').append(newConversationItem);
		var newConversation = "<div id='conversation"+n+"' class='innerPage'>"
							 +"<div class='conversationInputArea'><textarea id='conversationInput"
							 +n+"'></textarea><button id='conversationSubmit"+n+"' onclick='conversationPage.speak("+n
							 +")'>发送</button></div><div class='conversationRecordArea' id='conversationRecordArea"+n+"'></div></div>"
		jQuery('#conversationPage').append(newConversation);
		if(flag == 1){
			conversationPage.show();
			jQuery('#conversationList').css('display','none');
			jQuery('#conversation'+n).css('display','block');
			fixedTop.set('后退',user.friends[n].nickname,'');
			jQuery('#topLeftNav').unbind();
			jQuery('#topLeftNav').bind('click',function(){
				conversationPage.show();
			});
		}
		if(!user.friends[n].online){
			jQuery('#conversationName'+n).css('color','black');
		}
		else{
			jQuery('#conversationName'+n).css('color','rgb(255,215,0)');
		}
	}
}
var friendsPage = {
	init:function(){
		for(var i = 0; i < user.friendsAccount.length; i++){
			user.friends.push(friendModel.createNew(user.friendsAccount[i],'','',null,-1,false));
			tcp.handle.emit('askFriendsInfo',{'fAccount':user.friendsAccount[i],
											  'fIndex':i});
		}
		var allFriends = '';
		for(var i = 0; i < user.friendsAccount.length; i++){
			allFriends += "<div class='friends' onclick='conversationPage.talkWith("+i+",1)'> "
						  +"<a style='position:absolute;opacity:0' name='friends"+i+"'></a>"
						  +"<div class='friendsHead' id='friends"+i+"Head'></div>"
			              +"<div class='friendsNickname' id='friends"+i+"Nickname'></div>"
			              +"<div class='friendsAccount' id='friends"+i+"Account'></div>"
			              +"<div class='friendsSign' id='friends"+i+"Sign'></div></div>";
		}
		jQuery('#friendsPage').append(allFriends);
	},
	resize:function(){

	},
	show:function(){
		fixedTop.set('','好 友','查找');
		fixedTop.show();
		fixedBottom.show();
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			body.showPrompt('请输入对方的11位手机号','',function(target){
				if(!target){return;}
				if(target == user.account){
					body.showMessage('你找自己搞么子?',0);
					return;
				}
				var accountMode = /^[0-9]{11}$/;
				if(!accountMode.test(target)){
					body.showMessage('请输入正确的11位手机号',0);
					return;
				}
				for(var i = 0; i < user.friendsAccount.length; i++){
					if(user.friendsAccount[i] == target){
						body.showMessage('你们已经是好友啦，这不在这儿么?',1);
						document.location.hash = 'friends'+i;
						return;
					}
				}
				tcp.handle.emit('makeFriends',{'target':target});
				body.showMessage('好友请求已经发出，请等待回应',0);
			});
		});
		if(fixedTop.clicked){
			jQuery('#'+fixedTop.clicked).css('color','black');
		}
		fixedTop.clicked = 'friendsNav';
		jQuery('#'+fixedTop.clicked).css('color','rgb(255,215,0)');
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'friendsPage';
		jQuery('#'+body.currentPage).css('display','block');
	},
}
var gamePage = {
	resize:function(){

	},
	show:function(){
		fixedTop.set('','游 戏','选项');
		fixedTop.show();
		fixedBottom.show();
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			body.showMessage('game settings',0)
		});
		if(fixedTop.clicked){
			jQuery('#'+fixedTop.clicked).css('color','black');
		}
		fixedTop.clicked = 'gameNav';
		jQuery('#'+fixedTop.clicked).css('color','rgb(255,215,0)');
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'gamePage';
		jQuery('#'+body.currentPage).css('display','block');
	},
}
var albumPage = {
	//以后做一个长按图片后全屏显示的功能，并且能左右滑动浏览所有图片
	selectPP:-1,
	ppHtml:'',
	uploadPhotoHtml:"<div class='photo'><div id='uploadPhotoImg'>"
	    +"<form id='uploadPhotoForm'><input type='file' id='uploadPhotoInput' name='photo' onchange='albumPage.upload(this.files)'"
	    +"onclick='return albumPage.chooseFile()'/><input type='text' id='tcpId' name='id' style='display:none'/><input type='text'"
	    +"id='userAccount' style='display:none' name='account'/></form></div><div style='text-align:center;margin-top:10px;'>"
	    +"<progress id='uploadPhotoProgress' max=100 value=0 style='display:none;width:100%'></progress></div></div>",
	init:function(){
		albumPage.ppHtml = '';
		for(var i = 0; i < user.photos.length; i++){
			albumPage.ppHtml += "<div class='photo' id='pp"+i+"' style='background-image:url("+user.photos[i]
			+")' onclick='albumPage.select(event,"+i+")' ondblclick='albumPage.showPP("+i+")' /></div>";
		}
		jQuery('#albumPage').html(albumPage.ppHtml+albumPage.uploadPhotoHtml);
		jQuery('#tcpId').val(tcp.id);
		jQuery('#userAccount').val(user.account);
	},
	chooseFile:function(){
		clearTimeout(tcp.timingReload);
		tcp.timingReload = setTimeout(function(){
			body.needCheckLeave = false;
			window.location.reload();
		},75000);
		tcp.handle.emit('timingReply');
		return true;
	},
	resize:function(){

	},
	photoBlur:function(){
		fixedTop.set('返回','相 册','');
		jQuery('#topLeftNav').unbind();
		jQuery('#topLeftNav').bind('click',function(){
			settingsPage.show();
		});
		jQuery('#topRightNav').unbind();
	},
	showPP:function(i){
		body.showMessage('showPP ' + i,0);
	},
	select:function(event,i){
		var e = event||window.event;
		if(e.stopPropagation){
			e.stopPropagation();
		}
		else{
			e.cancelBubble = true;
		}
		albumPage.selectPP = i;
		fixedTop.set('使用','相 册','删除');
		jQuery('#topLeftNav').unbind();
		jQuery('#topLeftNav').bind('click',function(){
			user.headIndex = albumPage.selectPP;
			jQuery('#personHead').css('background-image','url('+user.photos[user.headIndex]+')');
			tcp.handle.emit('setHead',{'index':user.headIndex});
			body.showMessage('设置头像成功',1);
		});
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			if(user.headIndex == albumPage.selectPP){
				body.showConfirm('这可是你的头像，你确定要删除么？',function(){
					user.headIndex = -1;
					jQuery('#personHead').css('background-image','url(/images/headDefault.png)');
					user.photos.splice(albumPage.selectPP,1);
					albumPage.init();
					tcp.handle.emit('deletePhoto',{'ppIndex':albumPage.selectPP});
					albumPage.photoBlur();
				})
			}
			else{
				if(albumPage.selectPP < user.headIndex){
					user.headIndex--;
				}
				user.photos.splice(albumPage.selectPP,1);
				albumPage.init();
				tcp.handle.emit('deletePhoto',{'ppIndex':albumPage.selectPP});
				albumPage.photoBlur();
			}
		});
	},
	show:function(){
		albumPage.photoBlur();
		fixedTop.show();
		fixedBottom.show();
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'albumPage';
		jQuery('#'+body.currentPage).css('display','block');
	},
	upload:function(files){
		var imageMode = /^[\s\S]+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$/;
		if(!imageMode.test(files[0].name)){
			body.showMessage('请上传png,jpg,peg,gif,bmp格式的图像',0);
			return;
		}
		var xhr = new XMLHttpRequest();
		function photoUploadProgress(evt){
			document.getElementById('uploadPhotoProgress').value = Math.round((evt.loaded)*100/evt.total);
		}
		function photoUploadDown(){
			jQuery('#uploadPhotoProgress').css('display','none');
		}
		function photoUploadError(){
			body.showMessage('上传失败',0);
			jQuery('#uploadPhotoProgress').css('display','none');
		}
		function photoUploadCanceled(){
			body.showMessage('上传意外中止',0);
			jQuery('#uploadPhotoProgress').css('display','none');
		}
		xhr.upload.addEventListener('progress',photoUploadProgress,false);
		xhr.upload.addEventListener('load',photoUploadDown,false);
		xhr.upload.addEventListener('error',photoUploadError,false);
		xhr.upload.addEventListener('abort',photoUploadCanceled,false);
		xhr.open('POST','/uploadPhoto');
		var form = document.getElementById('uploadPhotoForm');
		xhr.send(new FormData(form));
		jQuery('#uploadPhotoProgress').css('display','block');
	},
}
var findBackPasswordPage = {
	resize:function(){

	},
	show:function(){
		body.showMessage('show findBackPasswordPage',0);
	}
}
var msgRecordPage = {
	resize:function(){

	},
	show:function(){
		body.showMessage('show findBackPasswordPage',0);
	}
}
var settingsPage = {
	changed:false,
	resize:function(){
	},
	change:function(){
		settingsPage.changed = true;
		jQuery('#topRightNav').css('color','red');
	},
	show:function(){
		fixedTop.set('','设 置','保存');
		fixedTop.show();
		fixedBottom.show();
		jQuery('#topRightNav').unbind();
		jQuery('#topRightNav').bind('click',function(){
			settingsPage.update();
		});
		if(fixedTop.clicked){
			jQuery('#'+fixedTop.clicked).css('color','black');
		}
		fixedTop.clicked = 'settingsNav';
		jQuery('#'+fixedTop.clicked).css('color','rgb(255,215,0)');
		jQuery('#'+body.currentPage).css('display','none');
		body.currentPage = 'settingsPage';
		jQuery('#'+body.currentPage).css('display','block');
	},
	update:function(){
		if(!settingsPage.changed){return;}
		jQuery('#topRightNav').css('color','white');
		var nickname = jQuery('#settingsNicknameInput').val();
		if(nickname == ''){
			body.showMessage('昵称不能为空',0);
			return;
		}
		var sign = jQuery('#settingsSignatureInput').val();
		tcp.handle.emit('updateInformation',{'nickname':nickname,'sign':sign})
		settingsPage.changed = false;
	},
	logout:function(){
		delete localStorage.dcjiaAccount;
		delete localStorage.dcjiaPassword;
		window.location.reload();
	}
}